// pages/minibee/minibee.js
var ConstantData = require('../../common/constant_data.js')
var util = require('../../utils/util.js')
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    name: null,
    beeAdvConfig: null,
  },

  getQueryVariable(url, variable) {
    var vars = url.split('?')[1].split("&");
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    }
    return false;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options && options.type != undefined) {
      this.setData({
        type: decodeURIComponent(options.type)
      })
    }
    if (options && options.name != undefined) {
      this.setData({
        name: decodeURIComponent(options.name)
      })
    }

    if (options && options.q != undefined) {
      //https://52kfz.com/mini-bee?type=0&name=renttime
      let type = this.getQueryVariable(decodeURIComponent(options.q), 'type');
      let name = this.getQueryVariable(decodeURIComponent(options.q), 'name');
      if (type) {
        this.setData({
          type: type
        })
      }
      if (name) {
        this.setData({
          name: name
        })
      }
    }

    // this.setData({
    //   type: 0,
    //   name: 'renttime'
    // })

    this.getConfig()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getConfig() {
    var that = this
    var systemInfo = wx.getStorageSync('systeminfo')
    var phoneType = ''
    if (systemInfo) {
      phoneType = systemInfo.model
    }
    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease/support/base_config',
        data: {
          phoneType: phoneType
        },
        success: function (res) {
          let netErr = app.handleRes(res, that);
          if (netErr) {
            return
          }
          var config = JSON.parse(res.data)
          let miniBeeConfig = config.miniBeeConfig
          console.log('【小蜜蜂广告配置】广告配置...' + JSON.stringify(miniBeeConfig))
          app.globalData.miniBeeConfig = miniBeeConfig
          // 小蜜蜂广告配置---end
          if (app.globalData.miniBeeConfig && app.globalData.miniBeeConfig.sub_message && app.globalData.miniBeeConfig.sub_message.length > 0) {
            for (let item of app.globalData.miniBeeConfig.sub_message) {
              if (item.name == that.data.name) {
                that.setData({
                  beeAdvConfig: item
                })
                break;
              }
            }
          } else {
            that.setData({
              beeAdvConfig: null
            })
          }
  
          if (!that.data.beeAdvConfig) {
            wx.reLaunch({
              url: '/pages/index/index',
            })
            return
          }
  
          if (that.data.type == 0) {
            util.bee_record_page_uv('模板消息-租借时长提醒', '/pages/minibee/minibee')
            util.bee_record_user_action(ConstantData.CONSTATNT_DATA.BASE_URL, -30004, '模板消息-租借时长提醒-小蜜蜂全屏广告', 'read')
          } else if (that.data.type == 1) {
            util.bee_record_page_uv('模板消息-收费账单提醒', '/pages/minibee/minibee')
            util.bee_record_user_action(ConstantData.CONSTATNT_DATA.BASE_URL, -30005, '模板消息-收费账单提醒-小蜜蜂全屏广告', 'read')
          }
  
        },
        fail(error) {
          console.log(error)
        }
      })

    });
    
  },

  goMiniBee() {
    var that = this
    wx.navigateToMiniProgram({
      appId: this.data.beeAdvConfig.appId,
      path: this.data.beeAdvConfig.path,
      success(res) {
        console.log('【小蜜蜂广告跳转】跳转成功：' + JSON.stringify(res))
        if (that.data.type == 0) {
          util.bee_record_user_action(ConstantData.CONSTATNT_DATA.BASE_URL, -30004, '模板消息-租借时长提醒-小蜜蜂全屏广告', 'success')
        } else if (that.data.type == 1) {
          util.bee_record_user_action(ConstantData.CONSTATNT_DATA.BASE_URL, -30005, '模板消息-收费账单提醒-小蜜蜂全屏广告', 'success')
        }
      },
      fail(error) {
        console.log('【小蜜蜂广告跳转】跳转失败：' + JSON.stringify(error))
        if (that.data.type == 0) {
          util.bee_record_user_action(ConstantData.CONSTATNT_DATA.BASE_URL, -30004, '模板消息-租借时长提醒-小蜜蜂全屏广告', 'fail')
        } else if (that.data.type == 1) {
          util.bee_record_user_action(ConstantData.CONSTATNT_DATA.BASE_URL, -30005, '模板消息-收费账单提醒-小蜜蜂全屏广告', 'fail')
        }
      }
    })
  },

})